package com.example.bzh.gamecat.ui.login;

import android.content.Intent;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.text.TextUtils;
import android.util.Log;
import android.view.View;
import android.view.Window;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import com.example.bzh.gamecat.R;
import com.example.bzh.gamecat.bean.UserBean;
import com.example.bzh.gamecat.dao.UserBeanDao;
import com.example.bzh.gamecat.ui.MainActivity;

import java.util.Map;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import butterknife.Unbinder;
import cn.smssdk.EventHandler;
import cn.smssdk.SMSSDK;

public class RegisterActivity extends AppCompatActivity {

    @BindView(R.id.iv_register_back)
    ImageView ivRegisterBack;
    @BindView(R.id.tv_register)
    TextView tvRegister;
    @BindView(R.id.register_num)
    EditText registerNum;
    @BindView(R.id.register_checknum)
    EditText registerChecknum;
    @BindView(R.id.btn_click_checknum)
    Button btnClickChecknum;
    @BindView(R.id.register_pwd)
    EditText registerPwd;
    @BindView(R.id.btn_register)
    Button btnRegister;
    @BindView(R.id.regist_catmao_click)
    ImageView registCatmaoClick;
    private Unbinder unbinder;
    private String checknum;
    private String phoneNum;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        supportRequestWindowFeature(Window.FEATURE_NO_TITLE);
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_register);
        unbinder = ButterKnife.bind(this);
        SMSSDK.initSDK(this, "1616d9212b6a1", "789fce3f8208236d3ca9f25966a9b015");


    }


    @OnClick({R.id.iv_register_back, R.id.btn_click_checknum, R.id.btn_register})
    public void onRegisterNum(View view) {
        switch (view.getId()) {
            case R.id.iv_register_back:
                finish();
                break;
            case R.id.btn_click_checknum:
                phoneNum = registerNum.getText().toString().trim();
                if (TextUtils.isEmpty(phoneNum)) {
                    Toast.makeText(this, "号码不能为空！", Toast.LENGTH_SHORT).show();
                } else {
                    SMSSDK.getVerificationCode("+86", phoneNum);
                    Toast.makeText(this, "发送成功:" + phoneNum, Toast.LENGTH_SHORT).show();
                }
                break;
            case R.id.btn_register:
                checknum = registerChecknum.getText().toString().trim();
                if (!TextUtils.isEmpty(checknum)) {
                    SMSSDK.submitVerificationCode("+86", phoneNum, checknum); //国家号，手机号码，验证码
                } else {
                    Toast.makeText(this, "验证码不能为空", Toast.LENGTH_SHORT).show();
                }
                SMSSDK.registerEventHandler(ev);
                break;
        }
    }

    //短信验证的回调监听
    private EventHandler ev = new EventHandler() {
        @Override
        public void afterEvent(int event, int result, Object data) {
            Log.i("TAG", "进入回调=");
            if (result == SMSSDK.RESULT_COMPLETE) {//回调完成
                Log.i("TAG", "回调完成=");
                Map<String, String> mData = (Map<String, String>) data;
                String country = (String) mData.get("country");//返回的国家编号
                String phone = (String) mData.get("phone");//返回用户注册的手机号
                Log.i("TAG", "phone=" + phone);
               // registCatmaoClick.setImageResource(R.mipmap.evaluate_checked);
                if (phone.equals(phoneNum)) {
                    //得到用户输入的密码
                    String passWord = registerPwd.getText().toString().trim();
                    UserBean user = new UserBean();

                    user.setPhone(phoneNum);
                    user.setPassword(passWord);
                    UserBeanDao.add(RegisterActivity.this, user);
                    runOnUiThread(new Runnable() {//更改ui的操作要放在主线程，实际可以发送hander
                        @Override
                        public void run() {
                            Toast.makeText(RegisterActivity.this, "通过验证", Toast.LENGTH_SHORT).show();
                        }
                    });
                    startActivity(new Intent(RegisterActivity.this, MainActivity.class));
                } else {
                    runOnUiThread(new Runnable() {//更改ui的操作要放在主线程，实际可以发送hander
                        @Override
                        public void run() {
                            Toast.makeText(RegisterActivity.this, "验证失败", Toast.LENGTH_SHORT).show();
                        }
                    });
                }

            }
        }
    };


    @Override
    protected void onDestroy() {
        super.onDestroy();
        if (unbinder != null) {
            unbinder.unbind();
        }
        //要在activity销毁时反注册，否则会造成内存泄露问题
        SMSSDK.unregisterAllEventHandler();
    }
}
